<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="130px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="任务" prop="questionnaireId">
            <el-select v-model="formItem.questionnaireId" placeholder="请选择任务" disabled style="width:100%">
              <el-option v-for="item in availableQuestionnaire" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工作序言" prop="preface">
            <el-input v-model="formItem.preface" placeholder="请输入工作序言" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="例行工作类（次）" prop="statsRoutineWork">
            <el-input-number v-model="formItem.statsRoutineWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="重保服务类（次）" prop="statsInsuranceWork">
            <el-input-number v-model="formItem.statsInsuranceWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="机房设施类（次）" prop="statsEnvWork">
            <el-input-number v-model="formItem.statsEnvWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="网络类（次）" prop="statsNetworkWork">
            <el-input-number v-model="formItem.statsNetworkWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="安全类（次）" prop="statsSecurityWork">
            <el-input-number v-model="formItem.statsSecurityWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="服务器类（次）" prop="statsServerWork">
            <el-input-number v-model="formItem.statsServerWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="存储设备类（次）" prop="statsStorageWork">
            <el-input-number v-model="formItem.statsStorageWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="资产管理类（次）" prop="statsAssetWork">
            <el-input-number v-model="formItem.statsAssetWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="文档管理类（次）" prop="statsDocumentWork">
            <el-input-number v-model="formItem.statsDocumentWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="其他辅助类（次）" prop="statsAssistWork">
            <el-input-number v-model="formItem.statsAssistWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col v-if="isWorker" :span="12">
          <el-form-item label="增值服务类（次）" prop="statsIncrementWork">
            <el-input-number v-model="formItem.statsIncrementWork" :precision="0" :step="1" :max="1000000000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工作亮点" prop="excellence">
            <el-input v-model="formItem.excellence" placeholder="工作亮点" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工作感想" prop="experience">
            <el-input v-model="formItem.experience" placeholder="工作感想" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标及计划" prop="target">
            <el-input v-model="formItem.target" placeholder="目标及计划" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="总结" prop="content">
            <el-input v-model="formItem.content" placeholder="总结" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" resize="none" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { detail, create, update, detailByTaskId } from '@neu/dcom-ui/api/questionnairefeedback'
import { getServiceSummaryOfWorker } from '@neu/dcom-ui/api/workDaily'
import { detail as taskDetail } from '@neu/dcom-ui/api/questionnaire'
import { list as searchQuestionnaire } from '@neu/dcom-ui/api/questionnaire'

const map = {
  statsRoutineWork: 'statsRoutineWork',
  A01: 'statsEnvWork',
  A02: 'statsNetworkWork',
  A03: 'statsSecurityWork',
  A04: 'statsServerWork',
  A05: 'statsStorageWork',
  A06: 'statsAssetWork',
  A07: 'statsDocumentWork',
  A08: 'statsAssistWork',
  A09: 'statsIncrementWork',
  statsInsuranceWork: 'statsInsuranceWork'
}

export default {
  name: 'QuestionnaireFeedbackForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    },
    taskId: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        id: '',
        questionnaireId: this.taskId > -1 ? this.taskId : '',
        preface: '',
        statsRoutineWork: 0,
        statsEnvWork: 0,
        statsNetworkWork: 0,
        statsSecurityWork: 0,
        statsServerWork: 0,
        statsStorageWork: 0,
        statsAssetWork: 0,
        statsDocumentWork: 0,
        statsAssistWork: 0,
        statsIncrementWork: 0,
        statsInsuranceWork: 0,
        excellence: '',
        experience: '',
        target: '',
        content: ''
      },
      // 表单验证规则
      rules: {
        questionnaireId: [{ required: true, message: '任务不能为空', trigger: 'blur' }],
        preface: [{ required: true, message: '工作序言不能为空', trigger: 'blur' }],
        excellence: [{ required: true, message: '工作亮点不能为空', trigger: 'blur' }],
        experience: [{ required: true, message: '工作感想不能为空', trigger: 'blur' }],
        target: [{ required: true, message: '目标及计划不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '总结不能为空', trigger: 'blur' }]
      },
      availableQuestionnaire: [],
      questionnaire: {}
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),
    isWorker: function() {
      return this.roles.some(r => r === 'worker')
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    },
    taskId: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
    searchQuestionnaire({ s_published: true }).then(response => {
      this.availableQuestionnaire = response.data
    })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
          this.getTaskDetail(this.formItem.questionnaire.id)
        })
      } else {
        detailByTaskId(this.taskId).then(response => {
          if (response.data) {
            this.formItem = {
              ...response.data
            }
          }
          this.getTaskDetail(this.taskId)
        })
      }
    },
    getTaskDetail(taskId) {
      taskDetail(taskId).then(response => {
        this.questionnaire = response.data
        this.getSummary(this.questionnaire.year, this.questionnaire.type)
      })
    },
    getSummary(year, type) {
      getServiceSummaryOfWorker(year, type).then(response => {
        response.data.forEach(item => {
          this.formItem[map[item.service_type_code]] = item.times
        })
      })
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.formItem.id !== ''
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.formItem.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
